Links.admin = {};

Links.admin._cbacks = {};

Links.admin.templates = null;
Links.admin.admin_man = null;

Links.admin.init = function ( dest_div )
{
	if ( ! dest_div ) dest_div = 'block_main';

	Links.admin.list_container ( function () {
		if ( ! Links.admin.templates )
		{
			liwe.AJAX.easy ( { action: "links.ajax.get_templates" }, function ( v ) {
				Links.admin.templates = v [ 'templates' ];
				Links.admin._admin_manager_init ( dest_div );

			} );
		}
	});
};

Links.admin._admin_manager_init = function ( dest_div )
{
	if ( ! Links.admin.admin_man )
	{
		Links.admin.admin_man = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "links.admin.search",
			'del_action': 'links.admin.delete'
		} );

		Links.admin.admin_man.set_row_fields ( [ 
							[ "Contenitore", "name" ], 
							[ "Sticky", "sticky" ], 
							[ "Testo", "testo" ], 
							[ "Url", "url" ]
						     ] );

		Links.admin.admin_man.cbacks [ 'row_manip' ] = Links.admin.list_row_manip;

		Links.admin.admin_man.cbacks [ 'edit_item' ] = function ( id ) { Links.admin.edit ( "am_footer", id ); };
		Links.admin.admin_man.cbacks [ 'del_item' ] =  function ( id ) { Links.admin.del ( "am_footer", id ); };
	}
};

Links.admin.list_row_manip = function ( ds, row )
{
	row [ '_id' ] = row [ 'id' ];
};

Links.admin.list_container = function ( cback )
{
	Links.admin._cnt_links = {};

	liwe.AJAX.easy ( { action: 'links.admin.list_container' }, function ( v ) {
		var cnts = v [ 'cnts' ];
		var t, l = cnts.length;

		for ( t = 0; t < l; t ++ )
			Links.admin._cnt_links [ cnts [ t ][ 'id' ] ] = cnts [ t ][ 'name' ];

		cback && cback ();
	} );
};


Links.admin.add_link = function ( dest )
{
	Links.admin._cbacks [ 'link_after_save' ] = Links.admin._set_link;

	Links.admin._set_link ( dest );
};

Links.admin._set_link = function ( dest_div, link )
{
	function _create_form ()
	{
		$ ( dest_div ).innerHTML = 	'<div id="filter_links"></div>' +
						'<div id="add_form_links"></div>';

		var f = Links.admin._create_set_link_frm ( link );
		f.set ( 'filter_links' );
	}

	if ( Links.admin._cnt_links.keys.length > 0 ) _create_form ();
	else
	{
		Links.admin.list_container ( function () 
		{
			_create_form ();
		} );
	}
};

Links.admin._create_opts_cnt_links = function ()
{
	var opts = [];

	Links.admin._cnt_links.iterate ( function ( v, k ) {
		opts.push ( { label: v, value: k } );
	} );

	return opts;
};

Links.admin._create_set_link_frm = function ( lnk )
{
	if ( ! lnk ) lnk = {};

	var f = new liwe.form.instance ( "frm_links_cnt" );


	var st = [
		{ label: 0, value: 0 },
		{ label: 1, value: 1 },
		{ label: 2, value: 2 },
		{ label: 3, value: 3 }
	];

	f.hidden ( "id_link", lnk.get ( 'id' ) );

	f.select ( { label: 'Contenitore', name: 'id_cnt', 
		options: [ { label: '', value: '' } ].concat ( Links.admin._create_opts_cnt_links () ), value: lnk.get ( 'id_cnt' ), mandatory: true } );

	f.text ( { label: 'Link', name: 'url', value: lnk.get ( 'url', '' ), mandatory: true } );
	f.text ( { label: 'Nome link', name: 'testo', value: lnk.get ( 'testo', '' ), mandatory: true } );
	f.checkbox ( { label: 'Apri nuova finestra', name: 'target', value: 1, checked: lnk.get ( 'target' ) ? true : false } );
	f.checkbox ( { label: 'Attivo', name: 'active', value: 1, checked: lnk.get ( 'active' ) ? true : false } );
	f.select ( { label: 'Sticky', name: 'sticky', options: st, value: lnk.get ( 'sticky', 0 ) } );

	f.button ( { value: 'Salva', name: 'btn_save', onclick: "Links.admin._set_links_save()" } );

	return f;
};

Links.admin._set_links_save = function ()
{
	var f = liwe.form.get ( "frm_links_cnt" );

	if ( ! f.check () )
	{
		alert ( "Riempire tutti campi" );
		return;
	}
	 
	var dct = f.get_values ();
	dct [ 'action' ] = "links.admin.create";

	liwe.AJAX.easy ( dct, function ( v )
	{
		if ( Links.admin._cbacks.get ( 'link_after_save' ) )
			Links.admin.admin_man.show_list ();//Links.admin._cbacks [ 'link_after_save' ] ();
	} );
};

Links.admin.list_links = function ( dest )
{
	Links.admin.admin_man.show_list ();

	/*
	function _create_form ()
	{
		$ ( 'block_main' ).innerHTML = 	'<div id="filter_links"></div>' +
						'<div id="res_list_links"></div>';

		Links.admin._create_filter_list_link_frm ();
	}

	if ( Links.admin._cnt_links.keys.length > 0 ) _create_form ()
	else
	{
		Links.admin.list_container ( function () 
		{
			_create_form ();
		} );
	}
	*/
};

Links.admin._create_filter_list_link_frm = function ()
{
	var f = new liwe.form.instance ( "list_links_frm" );

	f.hidden ( "action", "links.admin.search" );
	f.select ( { label: 'Contenitore', name: 'id_cnt', options: Links.admin._create_opts_cnt_links (), 
		onchange: "Links.admin._create_filter_list_link_frm_change()" 
	} );

	f.set ( "filter_links" );

	Links.admin._create_filter_list_link_frm_change ();
};

Links.admin._create_filter_list_link_frm_change = function ()
{
	var f = liwe.form.get ( "list_links_frm" );
	var dct = f.get_values ();


	liwe.AJAX.easy ( dct, function ( v )
	{
		var lnks = v [ 'links' ];
		var t, l = lnks.length;
		var s = '';

		for ( t = 0; t < l; t ++ )
		{
			var lnk = lnks [ t ];

			s += String.formatDict ( Links.admin.templates [ 'link_row' ], lnk );
		}

		if ( s ) s = Links.admin.templates [ 'link_header' ] + s + '</table>';

		$ ( 'res_list_links' ).innerHTML = s;

	} );
};

Links.admin.del = function ( dest_div, id )
{
	if ( ! id ) return;

	if ( confirm ( "Sei sicuro di eliminare l'elemento selezionato?" ) )
	{
		liwe.AJAX.easy ( { action: "links.admin.delete", ids: id }, function ( v )
		{
			Links.admin.edit_link_done ();
		} );
	}
};

Links.admin.edit = function ( dest_div, id )
{
	if ( ! id ) return;

	liwe.AJAX.easy ( { action: "links.admin.get", id_link: id }, function ( v )
	{
		Links.admin._cbacks [ 'link_after_save' ] = Links.admin.edit_link_done;
		Links.admin._set_link ( dest_div, v [ 'link' ] );
	} );
};

Links.admin.edit_link_done = function ()
{
	Links.admin.list_links ();
};
